package com.hotel.front;

import java.io.IOException;
import java.text.ParseException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.Util;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.gson.Gson;

import com.hotel.BL.POSMessagesLogic;

import data.MessagePOS;
import data.passingData.ResultData;

@SuppressWarnings("serial")
public class POSMessagesService extends HttpServlet{
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try{
			String doClass = req.getParameter("func");
			if(doClass.equals("getMessagePOS")){
				getAllPOSMessages(req,resp);
			}else if(doClass.equals("addMessagePOS")){
				AddPOSMessages(req,resp);
			}else if(doClass.equals("editMessagePOS")){
				EditMessagePOS(req,resp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	private void AddPOSMessages(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long hotelId = (Long) session.getAttribute("hotelId");
		String txtMessage = req.getParameter("txtMessage");
		String thxMessage = req.getParameter("thxMessage");
		Object obj = new POSMessagesLogic(req,resp).AddMessageFolioPOS(hotelId,txtMessage,thxMessage);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void EditMessagePOS(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, JSONException, ServletException,
			EntityNotFoundException, ParseException {
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long id = Long.parseLong(req.getParameter("id"));
		String txtMessage = (String) req.getParameter("txtMessage");
		String thxMessage = (String) req.getParameter("thxMessage");
		Object obj = new POSMessagesLogic(req,resp).EditMessagePOS(id, txtMessage,thxMessage);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	public void getAllPOSMessages(HttpServletRequest req, HttpServletResponse resp) throws IOException{
		HttpSession session = req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long hotelId = (Long) session.getAttribute("hotelId");
		List<MessagePOS> obj1 = new POSMessagesLogic(req,resp).GetMessagePOSByhotelId(hotelId);
		MessagePOS obj = null;
		if(obj1.size()!=0)
			obj = obj1.get(0);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}

}